package com.tiza.xiaoyoubao.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.tianze.library.adapter.recycler.RecyclerBaseAdapter;
import com.tianze.library.base.BaseStoreHouseRefreshRecyclerFragment;
import com.tianze.library.listener.SingleClickListener;
import com.tianze.library.view.EmptyLayout;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.AppConfig;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.app.UIHelper;
import com.tiza.xiaoyoubao.entity.AdvancesOnDoInfo;
import com.tiza.xiaoyoubao.entity.CommissionListInfo;
import com.tiza.xiaoyoubao.entity.UserInfo;
import com.tiza.xiaoyoubao.entity.bean.BankInfo;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.net.JsonCallback;
import com.tiza.xiaoyoubao.net.ResponseInfo;
import com.tiza.xiaoyoubao.utils.DateUtils;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by mapple on 17/2/17.
 */

public class CommissionFragment extends BaseStoreHouseRefreshRecyclerFragment {

    UserInfo userInfo = Hawk.get(Constants.KEY_USER_INFO, (UserInfo) null);

    private List<CommissionListInfo> infoList;

    @Override
    protected RecyclerBaseAdapter getListAdapter() {
        return new OrderListAdapter();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pageSize = Integer.MAX_VALUE;
        setRightActionText("提现");
        setRightActionListener(new SingleClickListener() {
            @Override
            public void onSingleClick(View view) {
                getBankInfo();
            }
        });
    }

    @Override
    protected void onItemClick(View view, int position) {
        super.onItemClick(view, position);
        CommissionListInfo info = (CommissionListInfo) mAdapter.getData().get(position);
        Bundle args = new Bundle();
        args.putSerializable("info", info);
        UIHelper.showCommissionDetial(getActivity(), args);
    }

    @Override
    protected void requestData(boolean refresh) {
        super.requestData(refresh);
        ApiHttpClient.getSelectCmiList(this, userInfo.getWebzzCmsBuyerInfo().getUserid(), new
                JsonCallback<CommissionListInfo>(
                        CommissionListInfo.class) {

                    @Override
                    public void onError(Call call, Exception e) {
                        onRequestError();
                    }

                    @Override
                    public void onResponse(ResponseInfo<CommissionListInfo> response) {
                        int code = response.getCode();
                        if (code == 1) {
                            mErrorLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
                            return;
                        }
                        infoList = response.getData();
                        if (infoList != null) {
                            Collections.sort(infoList,
                                    new Comparator<CommissionListInfo>() {
                                        @Override
                                        public int compare(
                                                CommissionListInfo commissionListInfo,
                                                CommissionListInfo t1
                                        ) {
                                            return (DateUtils.getDateToStringSec(t1
                                                    .getPayTime()))
                                                    .compareTo(
                                                            (DateUtils.getDateToStringSec(commissionListInfo.getPayTime())));
                                        }
                                    }
                            );
                        }
                        onRequestResponse(infoList);
                    }

                    @Override
                    public void onAfter() {
                        super.onAfter();
                        onRequestFinish();
                    }
                });
    }

    public static class OrderListAdapter extends RecyclerBaseAdapter {

        @Override
        protected View onCreateItemView(ViewGroup parent, int viewType) {
            return getLayoutInflater(parent.getContext()).inflate(R.layout.item_commission, parent,
                    false
            );
        }

        @Override
        protected ViewHolder onCreateItemViewHolder(View view, int viewType) {
            return new ViewHolder(viewType, view);
        }

        @Override
        public void onBindItemViewHolder(RecyclerBaseAdapter.ViewHolder holder, int position) {
            super.onBindItemViewHolder(holder, position);
            final ViewHolder vh = (ViewHolder) holder;
            CommissionListInfo info = (CommissionListInfo) _data.get(position);
            vh.tv_context.setText(info.getContent());
            vh.tv_commission.setText(info.getMoneyPay());
            vh.tv_balance.setText("余额:" + info.getCmi());
            vh.tv_time.setText(DateUtils.getDateToString(info.getPayTime()));

        }

        static class ViewHolder extends RecyclerBaseAdapter.ViewHolder {

            @BindView(R.id.tv_context)
            TextView tv_context;
            @BindView(R.id.tv_time)
            TextView tv_time;
            @BindView(R.id.tv_balance)
            TextView tv_balance;
            @BindView(R.id.tv_commission)
            TextView tv_commission;

            ViewHolder(int viewType, View view) {
                super(viewType, view);
                ButterKnife.bind(this, view);
            }
        }

    }

    private void getBankInfo() {
        ApiHttpClient.getBuyerBankInfo(this, userInfo.getWebzzCmsBuyerInfo().getUserid(),
                new JsonCallback<BankInfo>(BankInfo.class) {
                    @Override
                    public void onBefore(Request request) {
                        super.onBefore(request);
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        toast("网络异常,请稍后重试");
                    }

                    @Override
                    public void onResponse(ResponseInfo<BankInfo> response) {
                        int code = response.getCode();
                        String message = response.getMessage();
                        if (code == 1) {
                            showWarnDialog(message);
                            return;
                        }
                        List<BankInfo> data = response.getData();
                        if (null == data || data.size() == 0) {
                            toast("请先绑定银行卡");
                            return;
                        }
                        BankInfo bankInfo = data.get(0);
                        if (bankInfo.getAuth().equals("1")) {
                            Bundle args = new Bundle();
                            args.putSerializable("info", bankInfo);
                            args.putString("state", "commission");
                            UIHelper.showCommissionDeposit(mActivity, args);
                        } else {
                            toast("请先绑定银行卡");
                        }
                    }

                    @Override
                    public void onAfter() {
                        super.onAfter();
                    }
                }
        );
    }
}
